package top.itaso.app.common;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import top.itaso.app.R;

/**
 * @Description:
 * @Author dengfu.su
 * @Date 2022/11/8
 */
public class BaseDialogFragment extends DialogFragment {
    public final String TAG = this.getClass().getSimpleName();
    private LinearLayout layoutContent;
    private View layoutContentView;
    private TextView tvTitle;
    private TextView btnCancel;
    private TextView btnConfirm;

    private String mTitle;
    private String mCancel;
    private String mConfirm;
    private IOnCancelCallback mOnCancelCallback;
    private IOnConfirmCallback mOnConfirmCallback;

    public interface IOnCancelCallback {
        void onCancel();
    }

    public interface IOnConfirmCallback {
        void onConfirm();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.v(TAG, "onCreateView: >>>>>>" + TAG);
        View rootView = inflater.inflate(R.layout.layout_base_dialog_fragment, container, false);
        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        layoutContent = rootView.findViewById(R.id.layout_content);
        tvTitle = rootView.findViewById(R.id.tv_title);
        btnCancel = rootView.findViewById(R.id.btn_cancel);
        btnConfirm = rootView.findViewById(R.id.btn_confirm);

        if (!TextUtils.isEmpty(mTitle)) {
            tvTitle.setText(mTitle);
        }
        if (TextUtils.isEmpty(mCancel)) {
            btnCancel.setVisibility(View.GONE);
        } else {
            btnCancel.setText(mCancel);
            btnCancel.setVisibility(View.VISIBLE);
        }
        if (TextUtils.isEmpty(mConfirm)) {
            btnConfirm.setVisibility(View.GONE);
        } else {
            btnConfirm.setText(mConfirm);
            btnConfirm.setVisibility(View.VISIBLE);
        }
        if (layoutContentView != null) {
            layoutContent.addView(layoutContentView);
            layoutContent.setVisibility(View.VISIBLE);
        } else {
            layoutContent.setVisibility(View.GONE);
        }
    }

    public View setContentView(int layoutRes) {
        //如果使用外部传入的 LayoutInflater ，会导致 UI 上 switch 开关拉长
        View contentView  = LayoutInflater.from(getContext()).inflate(layoutRes, null);
//        View contentView = LayoutInflater.from(getContext()).inflate(layoutRes, layoutContent, false);
        return setContentView(contentView);
    }

    public View setContentView(View contentView) {
        this.layoutContentView = contentView;
        return contentView;
    }

    public void setTitleContent(String content) {
        mTitle = content;
        Log.d(TAG, "setTitleContent: >>>>>>弹出标题=" + mTitle);
    }

    public void onCancelButtonClick(String text, IOnCancelCallback cancelCallback) {
        mCancel = text;
        this.mOnCancelCallback = cancelCallback;
    }

    public void onConfirmButtonClick(String text, IOnConfirmCallback confirmCallback) {
        mConfirm = text;
        this.mOnConfirmCallback = confirmCallback;
    }
}
